%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Produce Figure 5
%%%%%%%%%%%%%%%%%%%%%


%Upper panels
 clear all; close all

para;

%%%%%%%%%%%%%%%%%%%%%
%%%% Modify base parameters
%%%%%%%%%%%%%%%%%%%%%%%
qBar=0.125;
aBar=0.125;
tolerance=0.000001;
delta=0.3;
Lambda=0.3;
r=0.05;
gamma=0.25;
phi=40;
kappa=40;
iComp=40;

%Iterate over phi
phiSpace=linspace(38,65,iComp);


for comp=1:iComp
    
    phi=phiSpace(comp);
    %Solve for Contract
    OptContract;
    SB;
    Solve;
    T=5;
    %Calculate beta in phi
    Time;
    vSuffCond(comp)=phi-1/(Lambda+r-qBar)/(gamma-r);
    indicator=0;


    %Calculate time t when beta_t\approx 0
    for t=1:length(vbetat)

        if vbetat(t)<0.001 && indicator==0

            vTSell(comp)=solVTime.x(t);
            indicator=1;
            break;

        end

    end
    %Save output in vector form
    indicator=1;
    aT=vAt(end);
    vVal(comp)=F0*(Lambda+r)-1;
    vCredit(comp)=tau0*Lambda-1;
    vCredit2(comp)=(tau0-tauB)/tauB;
    vCredit2(comp)=(tau0-tauB)/tauB;
    vQ(comp)=q;


end


%Plot
space=phiSpace;
[m,ind]=min(abs(vTSell-1));
cut=space(ind);

fFigure=figure(1); hold on;

 subplot(2,3,2); hold on
 plot(space,smooth(vCredit), 'k', 'Linewidth',3);
  plot([cut cut],[min(smooth(vCredit)) max(smooth(vCredit))], 'r:', 'Linewidth',1.5);
 xlabel('$\phi$','Interpreter','latex')
 ylabel('$\Delta^{Credit}$','Interpreter','latex')
 title('B: Change in Credit Risk (Total)','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;
 

 subplot(2,3,3); hold on
 plot(space,smooth(vCredit2), 'k', 'Linewidth',3);
  plot([cut cut],[min(smooth(vCredit2)) max(smooth(vCredit2))], 'r:', 'Linewidth',1.5);
 xlabel('$\phi$','Interpreter','latex')
 ylabel('$\Delta^{Credit}_{Mon}$','Interpreter','latex')
  title('C: Change in Credit Risk  (Monitoring)','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;

 subplot(2,3,1); hold on
 plot(space,smooth(vTSell), 'k', 'Linewidth',3);
   plot([cut cut],[min(smooth(vTSell)) max(smooth(vTSell))], 'r:', 'Linewidth',1.5);
 xlabel('$\phi$','Interpreter','latex')
 ylabel('$T^S$','Interpreter','latex')
  title('A: Time to Selloff','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;


resize_plot(fFigure,13.5,2.75,13);
print('ValueCreation','-depsc')


%Lower Panels

clear all; 


para;
%Modify base parameters
qBar=0.125;
aBar=0.125;
tolerance=0.000001;
delta=0.3;
Lambda=0.3;
r=0.05;
gamma=0.25;
phi=40;
kappa=40;
iComp=40;
gammaSpace=linspace(0.24,0.29,iComp);

suffCond=phi-1/(Lambda+r-qBar)/(gamma-r);

kappaSpace=linspace(37.5,42.5,iComp);
phiSpace=linspace(37.5,42.5,iComp);

%Iterate over gamma
for comp=1:iComp
    

    gamma=gammaSpace(comp);
    %Solve Contract
    OptContract;
    %Calculate beta
    SB;
    Solve;
    T=5;
    Time;
    vSuffCond(comp)=phi-1/(Lambda+r-qBar)/(gamma-r);
    indicator=0;
    for t=1:length(vbetat)

        if vbetat(t)<0.001 && indicator==0

            vTSell(comp)=solVTime.x(t);
            indicator=1;
            break;

        end

    end

    indicator=1;
    aT=vAt(end);
    vVal(comp)=F0*(Lambda+r)-1;
    vCredit(comp)=tau0*Lambda-1;
    vCredit2(comp)=(tau0-tauB)/tauB;
    vCredit2(comp)=(tau0-tauB)/tauB;
    vQ(comp)=q;

end

%Plot
space=gammaSpace;
[m,ind]=min(abs(vTSell-1));
cut=space(ind);

fFigure=figure(1); hold on;

 subplot(2,3,5); hold on
 plot(space,smooth(vCredit), 'k', 'Linewidth',3);
  plot([cut cut],[min(smooth(vCredit)) max(smooth(vCredit))], 'r:', 'Linewidth',1.5);
 xlabel('$\gamma$','Interpreter','latex')
 ylabel('$\Delta^{Credit}$','Interpreter','latex')
 title('E: Change in Credit Risk (Total)','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;
 

 subplot(2,3,6); hold on
 plot(space,smooth(vCredit2), 'k', 'Linewidth',3);
   plot([cut cut],[min(smooth(vCredit2)) max(smooth(vCredit2))], 'r:', 'Linewidth',1.5);
 xlabel('$\gamma$','Interpreter','latex')
 ylabel('$\Delta^{Credit}_{Mon}$','Interpreter','latex')
  title('F: Change in Credit Risk  (Monitoring)','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;

 subplot(2,3,4); hold on
 plot(space,smooth(vTSell), 'k', 'Linewidth',3);
    plot([cut cut],[min(smooth(vTSell)) max(smooth(vTSell))], 'r:', 'Linewidth',1.5);
 xlabel('$\gamma$','Interpreter','latex')
 ylabel('$T^S$','Interpreter','latex')
  title('D: Time to Selloff','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;


resize_plot(fFigure,13.5,5.25,13);
print('ValueCreation','-depsc')